#include "widget.h"
#include "login.h"

#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QDebug>
#include<QFile>
#include <QApplication>

int main(int argc, char *argv[])
{
     QApplication a(argc, argv);
    //qputenv("QT_IM_MODULE", QByteArray("Qt5Input"));
    QString dbName = "../industrial_-monitor/loginDb.db";
      // 检查数据库文件是否存在
      QFile dbFile(dbName);
      if (!dbFile.exists()) {
          // 如果数据库文件不存在，则创建它
          // 在这里，我们实际上不需要手动创建文件，因为QSqlDatabase在打开不存在的数据库时会自动创建
          qDebug() << "Database file does not exist. It will be created.";
      }



    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
    db.setDatabaseName(dbName);

    if (db.open()) {
        qDebug() << "数据库连接成功";

        QSqlQuery query;
        QString createTableQuery = "CREATE TABLE IF NOT EXISTS user ("
                                   "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                                   "account TEXT NOT NULL,"
                                   "password TEXT NOT NULL)";

        if (!query.exec(createTableQuery)) {
            qDebug() << "Error: Unable to create table";
            return -1;
        }

        qDebug() << "Database and table created successfully";
    } else {
        qDebug() << "数据库连接失败";
        qDebug() << db.lastError().text();
    }



   //main_page w;
    login w;
    w.show();
    return a.exec();
}
